package com.guoxue.uploadUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;

public class FileUtil {
	public static void main(String[] args) {
		Map<String, String> map = getListFiles();
		if (map==null||map.size()==0){
			return;
		}
		for (Map.Entry<String, String> m : map.entrySet()) {
			ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
			fixedThreadPool.execute(new Runnable() {
				public void run() {
					FileUtil.upload(m.getValue(), m.getKey());
				}
			});
		}
	}
	/**
	 * @param path
	 *            文件路径
	 * @param suffix
	 *            后缀名, 为空则表示所有文件
	 * @param isdepth
	 *            是否遍历子目录
	 * @return list
	 */
	public static Map<String,String> getListFiles() {
		String path = GetProperties.getResource().getProperty("path");
		String suffix = GetProperties.getResource().getProperty("suffix");
		String depth = GetProperties.getResource().getProperty("depth");
		String bucketName = GetProperties.getResource().getProperty("bucketName");
		String endpoint = GetProperties.getResource().getProperty("endpoint");
		String accessKeyId = GetProperties.getResource().getProperty("accessKeyId");
		String accessKeySecret = GetProperties.getResource().getProperty("accessKeySecret");
		if (path==null||path.equals("")){
			System.err.println("指定文件path路径为空");
			return null;
		}
		if (bucketName==null||bucketName.equals("")){
			System.err.println("!!!!!指定文件bucketName为空!!!!!");
			return null;
		}
		if (endpoint==null||endpoint.equals("")){
			System.err.println("!!!!!!指定文件endpoint为空!!!!");
			return null;
		}
		if (accessKeyId==null||accessKeyId.equals("")){
			System.err.println("!!!!!指定文件accessKeyId为空!!!!");
			return null;
		}
		if (accessKeySecret==null||accessKeySecret.equals("")){
			System.err.println("!!!!指定文件accessKeySecret为空!!!!");
			return null;
		}
		if (depth==null||depth.equals("")){
			System.err.println("!!!!指定文件depth为空!!!!");
			return null;
		}
		Boolean isdepth = null;
		if (depth.equals("true")){
			isdepth = true;
		} else if (depth.equals("false")){
			isdepth = false;
		} else {
			isdepth = false;
		}
		Map<String,String> lstFileNames = new HashMap<String, String>();
		File file = new File(path);
		if(!file.exists()){
			System.err.println("===============!!!!指定路径不存在!!!!!=====================");
			return null;
		}
		if (file.listFiles().length==0){
			System.err.println("===============!!!!指定文件夹为中无数据!!!!!=====================");
			return null;
		}
		return FileUtil.listFile(lstFileNames, file, suffix , isdepth);
	}
	private static Map<String,String> listFile(Map<String,String> Files, File f, String suffix, boolean isdepth) {
		// 若是目录, 采用递归的方法遍历子目录
		if (f.isDirectory()) {
			File[] t = f.listFiles();
			for (int i = 0; i < t.length; i++) {
				if (isdepth || t[i].isFile()) {
					listFile(Files, t[i], suffix, isdepth);
				}
			}
		} else {
			String fileName = f.getName();
			String path = f.getAbsolutePath();
			Files.put(fileName, path);
		}
		return Files;
	}
	
	private static void upload(String path,String key){
		String bucketName = GetProperties.getResource().getProperty("bucketName");
		String endpoint = GetProperties.getResource().getProperty("endpoint");
		String accessKeyId = GetProperties.getResource().getProperty("accessKeyId");
		String accessKeySecret = GetProperties.getResource().getProperty("accessKeySecret");
		OSSClient client =  new OSSClient(endpoint, accessKeyId, accessKeySecret);
		System.out.println("===========================================");
		System.out.println("Start to do bucket-related operations with OSS SDK for Java");
		System.out.println("===========================================\n");
		client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		try {
			client.putObject(bucketName, key, new File(path));
			System.err.println("文件上传成功:"+key);
		
		} catch (OSSException oe) {
			System.out.println("Caught an OSSException, which means your request made it to OSS, "
					+ "but was rejected with an error response for some reason.");
			System.out.println("Error Message: " + oe.getErrorCode());
			System.out.println("Error Code:       " + oe.getErrorCode());
			System.out.println("Request ID:      " + oe.getRequestId());
			System.out.println("Host ID:           " + oe.getHostId());
			System.err.println("文件上传失败:"+key);
		} catch (ClientException ce) {
			System.out.println("Caught an ClientException, which means the client encountered "
					+ "a serious internal problem while trying to communicate with OSS, "
					+ "such as not being able to access the network.");
			System.out.println("Error Message: " + ce.getMessage());
			System.err.println("文件上传失败:"+key);
			/*
			 * Do not forget to shut down the client finally to release all
			 * allocated resources.
			 */
			if (client != null) {
				client.shutdown();
			}
		}
	}
}
